package com.spelet.simulation;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.spelet.VariableCollection;
public class Level {
	public FileHandle fileHandle;
	public String[] allLevels;
	public Block[][] blocks;
	public TerrainBlock[][] terrainBlocks;
	public int rows;
	public int cols;
	public int startX;
	public int startY;
	public Array<Array<Block>> renderAtRows;
	public Array<TerrainBlock> yellowpillars;
	public Array<TerrainBlock> yellowbuttons;
	public Array<TerrainBlock> bluepillars;
	public Array<TerrainBlock> bluebuttons;
	public static String[] levelStrings;
	public static boolean[] levelsRead;
	public static boolean initiated;

	public Level(Simulation sim, String level){
		String[] l = level.split("Layer\n");
		String[] terrainBlock = l[0].split("\n");
		String[] blockisBlocks = l[1].split("\n");
		cols = terrainBlock[0].split(" ").length;
		rows = blockisBlocks.length;
		terrainBlocks = new TerrainBlock[cols][rows];
		blocks = new Block[cols][rows];
		if (yellowpillars == null) {
			yellowpillars = new Array<TerrainBlock>();
		} else {
			yellowpillars.clear();
		}
		if (bluepillars == null) {
			bluepillars = new Array<TerrainBlock>();
		} else {
			bluepillars.clear();
		}
		if (yellowbuttons == null) {
			yellowbuttons = new Array<TerrainBlock>();	
		} else {
			yellowbuttons.clear();
		}
		if (bluebuttons == null) {
			bluebuttons = new Array<TerrainBlock>();	
		} else {
			bluebuttons.clear();
		}
		renderAtRows = new Array<Array<Block>>();
		for (int i = 0; i < rows; i++) {
			renderAtRows.add(new Array<Block>());
		}
		String[] row;

		for(int y2 =0; y2< terrainBlock.length; y2++){
			row = terrainBlock[y2].split(" ");
			int y = terrainBlock.length-1-y2;
			for(int x =0; x < row.length; x++){
				if(row[x].equals("g")){
					terrainBlocks[x][y] = new TerrainBlock(VariableCollection.grass,x,y);
				} else if(row[x].equals("h")){
					terrainBlocks[x][y] = new TerrainBlock(VariableCollection.hole,x,y);
				} else if(row[x].equals("d")){
					terrainBlocks[x][y] = new TerrainBlock(VariableCollection.dirt,x,y);
				} else if(row[x].equals("yb")){
					terrainBlocks[x][y] = new TerrainBlock(VariableCollection.button,x,y);
					yellowbuttons.add(terrainBlocks[x][y]);
				} else if(row[x].equals("bb")){
					terrainBlocks[x][y] = new TerrainBlock(VariableCollection.button_blue,x,y);
					bluebuttons.add(terrainBlocks[x][y]);
				} 
				else if(row[x].equals("yp")){
					terrainBlocks[x][y] = new TerrainBlock(VariableCollection.pillar_yellow,x,y);
					yellowpillars.add(terrainBlocks[x][y]);
				}
				else if(row[x].equals("bp")){
					terrainBlocks[x][y] = new TerrainBlock(VariableCollection.pillar_blue,x,y);
					bluepillars.add(terrainBlocks[x][y]);
				}
			}
		}

		for(int y2 =0; y2< blockisBlocks.length; y2++){
			row = blockisBlocks[y2].split(" ");
			int y = blockisBlocks.length-1-y2;
			for(int x =0; x < row.length; x++){
				if(row[x].equals("w")){
					blocks[x][y] = new Block(VariableCollection.wall,x,y);
				} else if(row[x].equals("m")){
					blocks[x][y] = new Block(VariableCollection.moveBlock,x,y);
				} else if(row[x].equals("b")){
					blocks[x][y] = new Block(VariableCollection.boulder,x,y);
				} else if(row[x].equals("g")){
					blocks[x][y] = new Block(VariableCollection.goal,x,y);
				} else if(row[x].equals("n")){
					blocks[x][y] = new Block(VariableCollection.nothing,x,y);
				} else if (row[x].equals("c")) {
					startX = x;
					startY = y;
					blocks[x][y] = new Block(VariableCollection.nothing,x,y);
				} else if(row[x].equals("me")){
					blocks[x][y] = new Block(VariableCollection.mergeblock,x,y);
				}
				else if(row[x].equals("kr")){
					blocks[x][y] = new Block(VariableCollection.key_red,x,y);
				}
				else if(row[x].equals("kb")){
					blocks[x][y] = new Block(VariableCollection.key_blue,x,y);
				}
				else if(row[x].equals("ky")){
					blocks[x][y] = new Block(VariableCollection.key_yellow,x,y);
				}
				else if(row[x].equals("dr")){
					blocks[x][y] = new Block(VariableCollection.door_red,x,y);
				}
				else if(row[x].equals("db")){
					blocks[x][y] = new Block(VariableCollection.door_blue,x,y);
				}
				else if(row[x].equals("dy")){
					blocks[x][y] = new Block(VariableCollection.door_yellow,x,y);
				}		

				renderAtRows.get(y).add(blocks[x][y]);
			}
		}
		for (int x = 0; x < blocks.length; x++) {
			for (int y = 0; y < blocks[0].length; y++) {
				blocks[x][y].setSprite(x,y,blocks);
			}
		}
	}
}
